//
//  ConsumerListCell.m
//  XiaoMiao
//
//  Created by kinglian_iOS_000 on 2018/1/22.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "ConsumerListCell.h"
#import "ConsumerModel.h"

@interface ConsumerListCell ()
@property (weak, nonatomic) IBOutlet UIImageView *avatarImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIButton *photoButton;
@property (weak, nonatomic) IBOutlet UIButton *infoButton;
@property (weak, nonatomic) IBOutlet UIButton *avatarImageButton;

@end

@implementation ConsumerListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.avatarImageButton.layer.cornerRadius = 4.f;
    self.avatarImageButton.layer.masksToBounds = YES;

    [self.photoButton addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.infoButton addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.avatarImageButton addTarget:self action:@selector(avatarClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentViewDidTap)]];
}

- (void)setIsSearchType:(BOOL)isSearchType {
    _isSearchType = isSearchType;
    
    self.photoButton.hidden = !isSearchType;
    self.infoButton.hidden = !isSearchType;;
    
    if (isSearchType) {
        self.accessoryType = UITableViewCellAccessoryNone;
    }else {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
}


- (void)setConsumerModel:(ConsumerModel *)consumerModel {
    _consumerModel = consumerModel;
    
//    [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:consumerModel.profile] placeholderImage:kDefaultPlaceholdeImage];

    [self.avatarImageButton sd_setImageWithURL:[NSURL URLWithString:consumerModel.profile] forState:UIControlStateNormal placeholderImage:kDefaultPlaceholdeImage];
    self.nameLabel.text = consumerModel.realName;
}

- (void)btnDidClick:(UIButton *)sender {
    ConsumerListCellTapType tapType = 0;
    
    if (sender == self.photoButton) {
        tapType = ConsumerListCellTapTypePhoto;
    }else  if (sender == self.infoButton) {
        tapType = ConsumerListCellTapTypeInfo;
    }else {
        return;
    }
    
    if ([self.delegate respondsToSelector:@selector(consumerListCell:didTapType:)]) {
        [self.delegate consumerListCell:self didTapType:tapType];
    }
}

-(void)avatarClick{
    
    if (self.avatarClickBlock) {
        self.avatarClickBlock(self.consumerModel);
    }
}

- (void)contentViewDidTap {
    if (self.isSearchType) {
        return;
    }
    
    if ([self.delegate respondsToSelector:@selector(consumerListCell:didTapType:)]) {
        [self.delegate consumerListCell:self didTapType:ConsumerListCellTapTypeSelf];
    }
}

+ (NSString *)cellId {
    return @"_kConsumerListCell_";
}


+ (CGFloat)cellHeight {
    return 49.f;
}


@end
